package com.tm.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.Message;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Rocky
 * @create 2022-11-29 15:28
 */
public class ListSpliter {

    private final List<Message> msglist;

    private int currenIndex;

    private int minCount=50;

    public ListSpliter(ArrayList<Message> msglist) {
        this.msglist=msglist;
    }


    public boolean hasNext() {
        return currenIndex < this.msglist.size();
    }

    public List<Message> next() {
        int nextIndex =currenIndex;

        int totalsize=0;

        for (; nextIndex < msglist.size(); nextIndex++) {
            Message message = msglist.get(nextIndex);
            String s = JSONObject.toJSONString(message);
            if (s.length() > minCount){
                nextIndex++;
                break;
            }
            if(s.length() + totalsize < minCount){
                totalsize+=s.length();
            }else {
                break;
            }
        }
            List<Message> returnList=this.msglist.subList(currenIndex,nextIndex);
            currenIndex = nextIndex;
        return  returnList;
    }
}
